/**
 * Created with JetBrains WebStorm.
 * User: Администратор
 * Date: 8/22/13
 * Time: 10:15 PM
 * To change this template use File | Settings | File Templates.
 */
function DtpOnMap() {
    this.checkDtp = null;
    this.checkMaulfanction = null;
    this.checkOther = null;
    this.pointsDtp = null;
    this.fromPeriod = null;
    this.toPeriod = null;
    this.marker = null;


    this.close = function () {
        this.checkDtp = null;
        this.checkMaulfanction = null;
        this.checkOther = null;
        this.pointsDtp = null;
        this.fromPeriod = null;
        this.toPeriod = null;
        this.marker = null;
    }

    this.showDivSelect = function () {


        var scope = this;
        this.hideDtp();
        this.checkDtp = $('#checkDtp').is(':checked');
        this.checkMaulfanction = $('#checkMaulfanction').is(':checked');
        this.checkOther = $('#checkOther').is(':checked');

        if (document.getElementById("dtp_from").value != 'dd-mm-yy' && document.getElementById("dtp_to").value != 'dd-mm-yy') {
            if (this.checkDtp || this.checkMaulfanction || this.checkOther) {
                scope.fromPeriod = calcTime.parseCalendar(document.getElementById("dtp_from").value);
                scope.toPeriod = calcTime.parseCalendarPlus(document.getElementById("dtp_to").value);
                this.ajax(scope.fromPeriod, scope.toPeriod, scope.checkDtp, scope.checkMaulfanction, scope.checkOther)

            } else {
                alert('Не выбрано ни одно событие');
            }
        } else {
            alert('период не выбран');
        }


    };


    this.ajax = function (fromPeriod, toPeriod, checkDtp, checkMaulfanction, checkOther) {

        var pointsDtp = $.ajax({

            type:'POST',
            url:'DtpOnMap.php',
            data:'fromPeriod=' + fromPeriod +
                '&toPeriod=' + toPeriod +
                '&checkDtp=' + checkDtp +
                '&checkMaulfanction=' + checkMaulfanction +
                '&checkOther=' + checkOther +
                '&login=' + login +
                '&pass=' + pass,
            //посылаем интересующий нас день и номер юзера
            async:false,
            success:function (data) {
                //alert(data);
                //proverka_popoln();

            }

        }).responseText;
        if (pointsDtp == '0') {
            alert('Нет данных')
        } else {
            //alert(pointsDtp);
            this.drawOnMap(pointsDtp);
        }

    };

    this.drawOnMap = function (arrPoint) {
        var scope = this;
        var event = arrPoint.split('#!#');
        var lat, lng, msg, date;
        this.marker = [];


        for (var i = 0; i < event.length - 1; i++) {
            lat = event[i].split('#,#')[1];
            lng = event[i].split('#,#')[2];
            date = calcTime.unparse(event[i].split('#,#')[3]);
            msg = event[i].split('#,#')[5];
            if(this.checkDtp && event[i].split('#,#')[4]=='accident'){
                this.marker[i] = new L.Marker([lat, lng], {icon:dtpIcon}).bindPopup(date + '<br>' + msg);
                this.marker[i].addTo(map);
            }
            if(this.checkMaulfanction && event[i].split('#,#')[4]=='malfunction'){
                this.marker[i] = new L.Marker([lat, lng], {icon:malIcon}).bindPopup(date + '<br>' + msg);
                this.marker[i].addTo(map);
            }


        }

    };

    this.hideDtp = function () {
        var scope = this;
        if(this.marker){
            for (var i = 0; i < this.marker.length; i++) {
                if(this.marker[i]){
                    map.removeLayer(scope.marker[i]);
                }
            }
        }
        this.close();
    };


}
dtpOnMap = new DtpOnMap();
